/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servicios.DAO.impl;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import modelo.Articulo;
import servicios.DAO.ArticuloDAO;

/**
 *
 * @author epl
 */
public class ArticuloDAOToplink implements ArticuloDAO{
    private EntityManagerFactory emf;
    private EntityManager em;

    public void save(Articulo articulo) {
        em.getTransaction().begin();
        em.persist(articulo);
        em.getTransaction().commit();
    }

    public Articulo get(long id) {
        return em.find(Articulo.class, id);
    }

    public Articulo getByNumero(long numeroDeArticulo) {
        Query qt = em.createNamedQuery("findByNumero");
            qt.setParameter("numero", numeroDeArticulo);

        List<Articulo> articulos = qt.getResultList();
        return articulos.get(0);
    }

    public List<Articulo> getByDescripcion(String cadenaContenida) {
        Query qt = em.createNamedQuery("findByDescripcion");
        qt.setParameter("descripcion", "%" + cadenaContenida + "%");
        
        List<Articulo> articulos = qt.getResultList();
        return articulos;
    }

    public List<Articulo> getByPrecio(float precio) {
        Query qt = em.createNamedQuery("findByPrecio");
        qt.setParameter("precio", String.valueOf(precio));

        List<Articulo> articulos = qt.getResultList();
        return articulos;
    }


    public void dead() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
        public ArticuloDAOToplink (){
        emf = Persistence.createEntityManagerFactory("Shhh");
        em = emf.createEntityManager();
    }
}
